<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/7/6
 * Time: 11:11
 */
namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Validate;
use app\admin\model\Area;
class AreaController extends BaseController{
    //区域的增删改查
    public function lst(){
        $keyword = Request::instance()->param('keyword');
        if($keyword){
            $areas = Db::table('area')
                     ->where('name','like',"%$keyword%")
                     ->paginate(10,false,['query'=>['keyword'=>$keyword]]);
        }else{
            $areas = Db::table('area')
                ->paginate(10);
        }
//        dump($areas);
        $this->assign('areas',$areas);
        return $this->fetch();
    }
    //添加区域
    public function add(){
        if(Request::instance()->isPost()){
            //接收数据
         $data = [
             'pid'=>Request::instance()->param('pid') ? Request::instance()->param('pid') : 0,
             'name'=>Request::instance()->param('name'),
         ];
         $rule = [
             'name'=>'require',
         ];
          $msg = [
             'name.require'=>'区域名称不能为空',
          ];
            $validate = new Validate($rule,$msg);
            if($validate->check($data)){
                //添加数据
                $data['created_time'] = time();
                $data['updated_time'] = time();
                $re = Db::table('area')->insert($data);
                if($re){
                     $this->success('添加成功','area/lst');
                }else{
                     $this->success('添加失败','area/lst');
                }
            }else{
                $error = $validate->getError();
                $this->error($error);
            }

        }

        $areas = Db::table('area')->select();
        $area_list = Area::get_area_tree($areas,0,0);
        $this->assign('area_list',$area_list);
        return $this->fetch();
    }
    //编辑区域
    public function edit(){
        $id = Request::instance()->param('id');
        $area = Db::table('area')->where('id',$id)->find();
        if(Request::instance()->isPost()){
            //接收数据
            $data = [
                'id'=>Request::instance()->param('id'),
                'pid'=>Request::instance()->param('pid') ? Request::instance()->param('pid') : 0,
                'name'=>Request::instance()->param('name'),
            ];
            $rule = [
                'name'=>'require',
            ];
            $msg = [
                'name.require'=>'区域名称不能为空',
            ];
            $validate = new Validate($rule,$msg);
            if($validate->check($data)){
                //修改数据数据
                $data['updated_time'] = time();
                $re = Db::table('area')->where('id',$id)->update($data);
                if($re !== false){
                    $this->success('修改成功','area/lst');
                }else{
                    $this->success('修改失败','area/lst');
                }
            }else{
                $error = $validate->getError();
                $this->error($error);
            }

        }

        $areas = Db::table('area')->select();
        $area_list = Area::get_area_tree($areas,0,0);
        $this->assign('area_list',$area_list);
        $this->assign('area',$area);
        return $this->fetch();
    }
}